﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using DataLoader.UI.Misc;

namespace DataLoader.UI.Converters
{
    public class YesNoConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object returnValue = YesNo.N;

            int parsedInt;
            if (!Int32.TryParse(value.ToString(), out parsedInt))
                throw new InvalidOperationException("The source value must be an integer");

            if (parsedInt != 0 && parsedInt != 1)
                throw new InvalidOperationException("The source value must be a 0 or 1");

            if (parsedInt == 1) { returnValue = YesNo.Y; }
            if (parsedInt == 0) { returnValue = YesNo.N; }

            return returnValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int returnValue = 1;

            YesNo parsedValue = (YesNo) value;

            if (parsedValue == YesNo.Y)
                returnValue = 1;

            if (parsedValue == YesNo.N)
                returnValue = 0;

            return returnValue;
        }

    }
}